<?php

namespace App\Http\Controllers;

use App\Models\Component;
use App\Models\ComponentType;
use Filament\Facades\Filament;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\View\View;
use App\Models\Project;

class ComponentDetailController extends Controller
{
    public function detail(Request $request): View
    {
        $component = Component::with('ComponentTypes')->find($request->id);
        $component_key = $component->slug;
        $component->demo_data = json_decode($component->demo_data);
        $component->component_tree = json_decode($component->component_tree);
//        $component->preview_html = file_get_contents("http://qianduanku.test/demo/".$component_key."/index.html");
//        $component->preview_html = $this->formatPreviewHtml($request->id);

        $projects = Project::where('user_id', auth()->id())->get();

        return view('test', compact('component', 'projects'));
    }

    public function formatPreviewHtml($id)
    {
        $component = Component::find($id);
        $compiled_js = $component->compiled_js;
        $html = <<<HTML
    <!DOCTYPE html>
    <html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
    <h1>hello world this is the great world</h1>
    <script>
    $compiled_js
    </script>
</body>
HTML;
        return $html;
    }

    /**
     * through component id and framework type to get all component type ids in this component id
     * save component type id to component project
     * @param Request $request
     * @return array|true[]
     */

    public function saveComponentProject(Request $request)
    {
        $component = $request->component_type_id;

        $project = $request->project_id;

        $component_id = ComponentType::find($component)->component->id;

        $included_component_type_ids = ComponentType::find($component)->included_component_type_ids;

        if($included_component_type_ids == null){

            $exists = DB::table('component_project_type')->where('component_type_id', $component)->where('project_id', $project)->exists();

            if (!$exists) {
                DB::table('component_project_type')->insert([
                    'component_type_id' => $component,
                    'project_id' => $project,
                    'component_id' => $component_id,
                ]);

                return ['success' => true];
            }
        }else{

            array_push($included_component_type_ids, $component);

            foreach($included_component_type_ids as $included_component_type_id){
                $exists = DB::table('component_project_type')->where('component_type_id', $included_component_type_id)->where('project_id', $project)->exists();

                if(!$exists){
                    DB::table('component_project_type')->insert([
                        'component_type_id' => $included_component_type_id,
                        'component_id' => ComponentType::find($included_component_type_id)->component->id,
                        'project_id' => $project,
                    ]);

                    return ['success' => true];
                }
            }
        }

        return ['success' => true];
    }

    /**
     * save component to favorite
     */

    function saveToFavorite(Request $request)
    {
        $component = $request->component_id;

        $check = DB::table('component_favorite')->where([
            'component_id' => $component,
            'user_id' => auth()->id(),
        ]);

        if ($check->count() > 0) {
            return ['success' => false, 'message' => '该组件已被收藏!'];
        }

        DB::table('component_favorite')->insert([
            'component_id' => $component,
            'user_id' => auth()->id(),
        ]);

        return ['success' => true];
    }

}
